<template>
  <!-- 备注表单 -->
  <a-modal
    v-model:open="visible"
    title="设置备注"
    class="addStation"
    destroyOnClose
    :width="450"
    @cancel="cancel"
    centered
  >
    <a-form
      :model="form"
      :rules="rules"
      layout="inline"
      :labelCol="labelCol"
      ref="formRef"
    >
      <a-form-item label="备注" name="remark">
        <a-textarea
          v-model:value="form.remark"
          class="formInput"
          placeholder="请输入"
          :rows="4"
        />
      </a-form-item>
    </a-form>
    <template #footer>
      <div class="footerBar">
        <a-button key="back" @click="cancel">取消</a-button>
        <a-button
          key="submit"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确认</a-button
        >
      </div>
    </template>
  </a-modal>
</template>
<script setup>


const $api = inject("$api");
const props = defineProps({
  addRemarkVisible: {
    type: Boolean,
    default: false,
  },
  rowData: {
    type: Object,
  },
  isEdit: {
    type: Boolean,
    default: false,
  },
});
const loading = ref(false);
const visible = ref(props.addRemarkVisible);
const emits = defineEmits(["update:addRemarkVisible", "getCompageList"]);
watch(
  () => props.addRemarkVisible,
  (val) => {
    visible.value = val;
    if (val) {
      if (props.isEdit) {
        form.value = {
          ...props.rowData,
        };
      }
    }
  }
);
const labelCol = { style: { width: "70px" } };
const formRef = ref(null);
const form = ref({
  remark: null,
});
const rules = ref({
  remark: [
    {
      required: true,
      message: "请输入",
      trigger: "blur",
    },
  ],
});

const cancel = () => {
  emits("update:addRemarkVisible", false);
  form.value = {
    remark: null,
  };
  formRef.value.clearValidate();
};
const handleSubmit = () => {
  formRef.value
    .validate()
    .then(async () => {
      const params = {
        ...form.value,
        id: props.rowData.id,
      };
      loading.value = true;
      const res = await $api.companyUpdate(params, "put");
      loading.value = false;
      if (res.code === 0) {
        message.success("编辑成功");
        cancel();
        emits("getCompageList");
      }
    })
    .catch((error) => {
      console.log("error", error);
    });
};
</script>

<style lang="less" scoped>
.form {
  padding: 0 40px;
  margin-top: 30px;
}
.formInput,
.formSelect {
  width: 300px;
}
.ant-form-item {
  margin-top: 20px;
}
.footerBar {
  text-align: center;
  margin-top: 30px;
}
.areaSelect {
  display: inline-block;
  width: 113px;
  margin-right: 5px;
  &:last-child {
    margin-right: 0;
  }
}
</style>